package com.study.talk.netty;

import com.study.talk.netty.message.MyChannelInitializer;
import com.study.talk.netty.message.TalkMessage;
import com.study.talk.util.Constants;
import com.study.talk.util.EventBusUtil;
import com.study.talk.util.OtherMessage;

import java.util.concurrent.Executors;

public class ConnectTest {

    public static AndroidNetty netty = new AndroidNetty(new MyChannelInitializer());

    public static void start(){
        netty.connect();
    }

    public static void sendMessage(TalkMessage message) {

        //连接还在，发送消息
        if (netty.isConnected()) {
            netty.sendMessage(message);
        } else {
            //全局通知断开连接
            EventBusUtil.sendEvent(new OtherMessage(Constants.EVENT_NETTY_DISCONNECT));
        }
    }

    public static void close() {
        Executors.newSingleThreadExecutor().submit(() -> {
            netty.close();
        });
    }

    public static void disconnect(){
        netty.disconnect();
    }
}
